package com.fwindcore.stage;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.renderers.BatchTiledMapRenderer;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.fwindcore.res.FontFactory;
import com.fwindcore.res.ImageFactory;
import com.fwindcore.res.PublicConst;
import com.fwindcore.util.ScreenPos;
import com.fwindcore.wheretorun.MyGdxGame;

public class GameOverStage extends Stage {
	
	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		((MyGdxGame)game).reStart();
		return super.touchDown(screenX, screenY, pointer, button);
	}

	private Image bgImg;
	private BitmapFont font;
	private boolean isWin;
	private int step;
	private SpriteBatch batch;
	private Game game;

	public GameOverStage(Game game,boolean isWin,int step) {
		// TODO Auto-generated constructor stub
		this.create();
		
		this.game = game;
		this.init();
		this.addActor();
		this.isWin = isWin;
		this.step = step;
		
	}

	private void create(){
		bgImg = ImageFactory.getGameOverImg();
		
		font = FontFactory.getFont32();
		batch  = this.getSpriteBatch();
		
	}
	
	private void init(){
		bgImg.setPosition(ScreenPos.getCenterX(bgImg.getWidth()), ScreenPos.getCenterY(bgImg.getHeight()));
		
		Gdx.input.setCatchBackKey(true);
		Gdx.input.setInputProcessor(this);
		
	}
	
	private void addActor(){
		this.addActor(bgImg);
//		this.addActor(font);
	}
	
	@Override
	public boolean keyDown(int keyCode) {
		// TODO Auto-generated method stub
		System.out.println("keyDown:" + keyCode);

		if (keyCode == 4 || keyCode == 131) {
			((MyGdxGame)game).reStart();
		}

		return super.keyDown(keyCode);
	}
	
	@Override
	public void act() {
		// TODO Auto-generated method stub
		Gdx.input.setInputProcessor(this);
		
		super.act();
	}
	
	@Override
	public void draw() {
		// TODO Auto-generated method stub
		super.draw();
		
		String str1 = "You "+ (isWin?"Win":"Lost") + "!";
		String str2 = "Steps:"+step;
		
		float x = bgImg.getX(),y = bgImg.getY(),w = bgImg.getWidth(),h = bgImg.getHeight();
		
		batch.begin();
		
		font.draw(batch, str1,x+(w-str1.length()*font.getSpaceWidth())/2,y+h-60);
		font.draw(batch, str2,x+(w-str2.length()*font.getSpaceWidth())/2,y+h-150);	
		
		batch.end();
		
	}

}
